﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SqlHelperForCry.Common;
using Torch.WL.Entity;



namespace Torch.WL.Web.CleanChina
{
    public partial class company : BasePage
    {
        protected int gsId
        {
            get
            {
                int _gsId = -1;
                int.TryParse(hfGsId.Value, out _gsId);
                return _gsId;
            }

        }

        private string imageUrl
        {
            get
            {
                if (ViewState["WebsiteImageUrl"] == null)
                    ViewState["WebsiteImageUrl"] = AppSettingsHelper.GetString("WebsiteImageUrl");
                string url = ViewState["WebsiteImageUrl"].ToString();
                return url;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            int recordCount = 0;
            string whereClause = " 1=1 ";
            if (!string.IsNullOrEmpty(this.txtGsmc.Text.Trim()))
                whereClause += " and companyName like '%" + this.txtGsmc.Text.Trim().Replace("'", "") + "%'";
            DataTable dt = DataAccess.Select(typeof(Tb_Company), this.pgList.PageIndex, this.pgList.PageSize, whereClause, "createTime", ref recordCount);
            dt.Columns.Add("Gslogo", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["companyLogo"] != null && dr["companyLogo"] != DBNull.Value)
                    dr["Gslogo"] = "<img src='" + imageUrl + "\\" + dr["companyLogo"] + "' style='width:40px;height:40px;'>";
            }
            this.rptZs.DataSource = dt;
            this.rptZs.DataBind();

            pgList.RecordCount = recordCount;
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            pgList.PageIndex = 0;
            BindData();
        }


        protected void lbtnSaveZs_Click(object sender, EventArgs e)
        {
            Tb_Company gs = new Tb_Company(gsId);
            gs.Load();
            string fileName = "";
            string msg = "";
            if (!WebCommon.UploadFile(this.fuGslogo, out fileName, out msg))
                WebCommon.ShowMessage(this, msg);
            else
            {
                if (!string.IsNullOrEmpty(fileName))
                    gs.companyLogo = fileName;
            }
            if (gs == null)
                gs.createTime = DateTime.Now;

            gs.companyMemo = this.txt_Zs_Jj.Text;
            gs.companyName = this.txt_Zs_Gsm.Text;
            gs.firstPingYinChar = WebCommon.ChineseCap(this.txt_Zs_Gsm.Text.Trim());
            gs.companyTele = this.txt_Zs_Dh.Text;
            gs.product = this.txt_Zs_Zycp.Text;
            gs.ZhanWeiH = this.txt_Zs_Zwh.Text;
            gs.Save();

            WebCommon.CloseDialog(this, "Zs_dialog");
            BindData();
            this.upGsList.Update();
            WebCommon.ShowMessage(this, "已成功保存！");
        }

        protected void rptZs_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int gsId = 0;
                int.TryParse(e.CommandArgument.ToString(), out gsId);
                Tb_Company gs = new Tb_Company(gsId);
                gs.Delete();
                BindData();
                this.upGsList.Update();
                WebCommon.ShowMessage(this, "已成功删除！");
            }
            if (e.CommandName == "Edit")
            {
                int gsId = 0;
                int.TryParse(e.CommandArgument.ToString(), out gsId);
                Tb_Company gs = new Tb_Company(gsId);
                if (gs.Load())
                {
                    this.ltGslogo.Text = "<img src='" + imageUrl + "\\" + gs.companyLogo + "' style='width:40px;height:40px;'>";
                    this.txt_Zs_Dh.Text = gs.companyTele;
                    this.txt_Zs_Gsm.Text = gs.companyName;
                    this.txt_Zs_Jj.Text = gs.companyMemo;
                    this.txt_Zs_Zwh.Text = gs.ZhanWeiH;
                    this.txt_Zs_Zycp.Text = gs.product;
                    this.hfGsId.Value = gsId.ToString();
                    upZsDetail.Update();
                    WebCommon.ShowDialog(this, "Tjzs");
                }

            }
        }

        protected void pgList_OnPreRender(object sender, EventArgs e)
        {
            BindData();
        }
    }
}